// EnumGruppe.cpp : Implementation of CEnumGruppe
#include "stdafx.h"
#include "EBASdll.h"
#include "EnumGruppe.h"
#include "Gruppe.h"

/////////////////////////////////////////////////////////////////////////////
// CEnumGruppe

STDMETHODIMP CEnumGruppe::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IEnumGruppe
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

CEnumGruppe::CEnumGruppe()
{
	m_pUnkMarshaler = NULL;
	m_records = NULL;
}

CEnumGruppe::~CEnumGruppe()
{
	if (m_records)
		m_records->Close();
}

STDMETHODIMP CEnumGruppe::Next(/*[out]*/ IUnknown ** ppUnk, /*[out]*/ ULONG * pcFetched)
{
	_CommandPtr cmd;
	
	const char sql[255] = "EBAS_SP_HENT_GRUPPE";

	try {
		if (!ppUnk)
			return E_POINTER;

		ULONG cFetched = 0;

		if (m_first) {
			m_first = FALSE;

			cmd.CreateInstance(__uuidof(Command));
			cmd->ActiveConnection = CDatabase::GetConnection();
			cmd->CommandText = sql;
			cmd->CommandType = adCmdStoredProc;
			cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

			m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
			if (m_records->ADO_EOF)
				return E_ACCESSDENIED;
		}			

		if (m_records->ADO_EOF)
			return S_OK;

		m_length = (long)m_records->Fields->GetItem("ANTAL")->Value;

		CComObject<CGruppe> * pInstance = NULL;
		CComObject<CGruppe>::CreateInstance(&pInstance);

		pInstance->put_GUID( (bstr_t)m_records->Fields->GetItem("GUID")->Value );
		pInstance->put_Navn( (bstr_t)m_records->Fields->GetItem("NAVN")->Value );
		pInstance->put_StartTid( (bstr_t)m_records->Fields->GetItem("START_TID")->Value );
		pInstance->put_SlutTid( (bstr_t)m_records->Fields->GetItem("SLUT_TID")->Value );
		pInstance->put_Sted( (bstr_t)m_records->Fields->GetItem("STED")->Value );
		pInstance->put_Bemaerkning( (bstr_t)m_records->Fields->GetItem("BEMAERKNING")->Value );
		pInstance->put_Ejerforhold( (bstr_t)m_records->Fields->GetItem("EJERFORHOLD_GUID")->Value );

		pInstance->QueryInterface(ppUnk);
		m_pos += 1;
		cFetched = m_length;

		if (pcFetched)
			*pcFetched = cFetched;

		m_records->MoveNext();

	} catch (_com_error &e) {
		char err[512];
		strncpy(err, e.ErrorMessage(), 512);
		strncpy(err, " ", 1);
		strncpy(err, e.Description(), _tcslen(e.Description()));
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumGruppe::Skip(ULONG cItems)
{
	try {
		if (m_records) {
			m_pos += cItems;
			m_records->Move(m_pos);
		}
	} catch (_com_error &e) {

		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumGruppe::Reset()
{
	_CommandPtr cmd;

	const char sql[255] = "EBAS_SP_HENT_GRUPPE";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;	
	} catch (_com_error &e) {
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumGruppe::Clone(IEnumeration ** ppEnumeration)
{
	return E_NOTIMPL;
}

STDMETHODIMP CEnumGruppe::findGruppeNavn(BSTR navn, IGruppe **pGruppe)
{
	_CommandPtr cmd;
	_RecordsetPtr recordset;
	
	const char sql[255] = "EBAS_SP_HENT_GRUPPE";

	try {
		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("navn", adVarChar, adParamInput, 255, navn) );

		recordset = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (recordset->ADO_EOF)
			return E_ACCESSDENIED;

		CComObject<CGruppe> * pInstance = NULL;
		CComObject<CGruppe>::CreateInstance(&pInstance);

		pInstance->put_GUID( (bstr_t)recordset->Fields->GetItem("GUID")->Value );
		pInstance->put_Navn( (bstr_t)recordset->Fields->GetItem("NAVN")->Value );
		pInstance->put_StartTid( (bstr_t)recordset->Fields->GetItem("START_TID")->Value );
		pInstance->put_SlutTid( (bstr_t)recordset->Fields->GetItem("SLUT_TID")->Value );
		pInstance->put_Sted( (bstr_t)recordset->Fields->GetItem("STED")->Value );
		pInstance->put_Bemaerkning( (bstr_t)recordset->Fields->GetItem("BEMAERKNING")->Value );
		pInstance->put_Ejerforhold( (bstr_t)recordset->Fields->GetItem("EJERFORHOLD_GUID")->Value );

		pInstance->QueryInterface(__uuidof(IGruppe), (void**)pGruppe);

		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CEnumGruppe::findGrupperStuderende(IStuderende *pStud)
{
	_CommandPtr cmd;
	
	const char sql[255] = "EBAS_SP_HENT_GRUPPE";

	try {
		BSTR stud;
		_variant_t var;
		var.vt = VT_NULL;

		pStud->get_GUID(&stud);	

		m_first = FALSE;

		cmd.CreateInstance(__uuidof(Command));
		cmd->ActiveConnection = CDatabase::GetConnection();
		cmd->CommandText = sql;
		cmd->CommandType = adCmdStoredProc;
		cmd->Parameters->Append( cmd->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		cmd->Parameters->Append( cmd->CreateParameter("navn", adVarChar, adParamInput, 255, var) );
		cmd->Parameters->Append( cmd->CreateParameter("studGuid", adVarChar, adParamInput, 255, bstr_t(stud)) );

		m_records = cmd->Execute(NULL, NULL, adCmdStoredProc);	
		if (m_records->ADO_EOF)
			return E_ACCESSDENIED;

	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}
